<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Training extends CI_Controller {
	 function __construct()
	 {
	   parent::__construct();
	   $this->load->model('muser');
	   $this->load->library('form_validation');
	   $this->load->library('pagination');
	   $this->load->library('my_datatables');
	 }
	 function index(){
	 	 if ($this->input->post('Training_ID')!=NULL )
	 	 {
	 	 	$this->form_validation->set_rules('Training_ID','Training_ID','trim|integer|callback_check_Training');
			$this->form_validation->set_rules('Traning_Mode','Traning_Mode','trim|required');
			$this->form_validation->set_rules('CourseName','CourseName','trim|required');
			$this->form_validation->set_rules('TRN_Provider','TRN_Provider','trim|required');
			$this->form_validation->set_rules('PlanFromDate','PlanFromDate','trim|required|callback_Validate[yyyy/mm/dd]');
			$this->form_validation->set_rules('PlanToDate','PlanToDate','trim|required|callback_Validate[yyyy/mm/dd]');
			$this->form_validation->set_rules('ActFromDate','ActFromDate','trim|required|callback_Validate[yyyy/mm/dd]');
			$this->form_validation->set_rules('ActToDate','ActToDate','trim|required|callback_Validate[yyyy/mm/dd]');
			$this->form_validation->set_rules('TRN_Values','TRN_Values','trim|integer|required');
					  		
			if ($this->form_validation->run()==FALSE){
				$data['error']='Training_ID or other incorrect!';
			}else{
				$DT=array(
						'Training_ID'=>$this->input->post('Training_ID'),
						'Training_Mode'=>$this->input->post('Training_Mode'),
						'CourseName'=>$this->input->post('CourseName'),
						'TRN_Provider'=>$this->input->post('TRN_Provider'),
						'PlanToDate'=>$this->input->post('PlanToDate'),
						'PlanFromDate'=>$this->input->post('PlanFromDate'),
						'ActFromDate'=>$this->input->post('ActFromDate'),
						'ActToDate'=>$this->input->post('ActToDate'),
				);
				$this->muser->addData('Training',$DT);
				$data['error']='insert successful!';
			} 	
				
	 	 }	
	 	 $config['base_url'] = base_url().'training/index'; 
		 $config['total_rows'] = $this->muser->num_rows('Training');
		 
		 $config['per_page'] = 5; // xác định số record ở mỗi trang  
	     $config['uri_segment'] = 3; // xác định segment chứa page number  
		 $this->pagination->initialize($config); 
		 $data['result']=$this->muser->getalldata('Training',$config['per_page'],$this->uri->segment(3));	
		 
		 $this->load->view('_header',$data);
	     $this->load->view('training/training_view',$data); 
	     $this->load->view('_footer');
	 }
	 function delete(){
	 	$id = $_POST['id'];
		$this->muser->delColumn('Training','Training_ID',$id);
		//redirect('training/index');
	 }
	 
	 function edit(){
	 	$editLabID=$this->session->userdata('Lab_ID');
		redirect('/lab/edit',$editLabID);
	 }
	 
	 function check_Training($str){
	 		return $this->muser->checkData('Training','Training_ID',$str);		
	 }
	 
	 function table(){
	 	$this->my_datatables
	 		->select('Training_ID, TRN_Mode, CourseName, TRN_Provider, PlanFromDate, PlanToDate, ActFromDate, ActToDate, TRN_Values')
			->from('Training');
			//->join('Staff','Staff.Staff_ID=WorkDay.Staff_ID')
			//->select('FullName');
		echo $this->my_datatables->generate();
	 }
 
	 function update(){
	 	$id = $_POST['id'];
		$columnName = $this->getRealColumnName($_POST['columnName']);
		$data[$columnName] = $_POST['value'];
		
		$this->muser->updateData('Training','Training_ID',$id,$data);
		echo $data[$columnName];
	 }
	 
	 function add(){
			/*
			$row = $this->muser->getMaxId();
							  foreach ($row as $i) {
								  $newData['Training_ID'] = $i + 1;
					}*/
			$newData['TRN_Mode'] = $_POST['TRN_Mode'];
			$newData['CourseName'] = $_POST['CourseName'];
			$newData['TRN_Provider'] = $_POST['TRN_Provider'];
			$newData['PlanFromDate'] = $_POST['PlanFromDate'];
			$newData['PlanToDate'] = $_POST['PlanToDate'];
			if ($_POST['ActFromDate'] === ''){
				$newData['ActFromDate'] = NULL;
			}
			else {
				 $newData['ActFromDate'] = $_POST['ActFromDate'];
			 }
			 if ($_POST['ActToDate'] === ''){
				 $newData['ActToDate'] = NULL;
			 }
			 else {
				 $newData['ActToDate'] = $_POST['ActToDate'];
			 }
			$newData['TRN_Values'] = $_POST['TRN_Values'];
			$this->muser->addData('Training',$newData);
	 }
 
	 function getRealColumnName($aliasName){
	 	switch ($aliasName) {
		    case 'ID':
		        return 'Training_ID';
		    case 'TRN Mode':
		        return 'TRN_Mode';
		    case 'Course Name':
		        return 'CourseName';
			case 'Provider':
		        return 'TRN_Provider';
			case 'Plan From':
		        return 'PlanFromDate';
			case 'Plan To':
		        return 'PlanToDate';
			case 'Act From':
		        return 'ActFromDate';
			case 'Act To':
		        return 'ActToDate';
			case 'Value':
		        return 'TRN_Values';
		}
	 }
 
	 function assign($courseId){
	 	if ($courseId != null)
		 	$this->session->set_userdata('courseId', $courseId);
	 	//echo $courseId;
	 	$this->load->view('_header');
	    $this->load->view('training/trainingassign_view'); 
	    $this->load->view('_footer');
	 }
	 
	 function addstafftocourse()
	 {
	 	$dt['Staff_ID'] = $_POST['Staff_ID'];
	 	$dt['Training_ID'] = $_POST['Training_ID'];
		$dt['TRN_Achivement'] = NULL;
		$dt['Actual'] = NULL;
		
		if ($this->muser->isStaffTrainingExisted($dt['Training_ID'], $dt['Staff_ID']))
		{
			//return FALSE;
			//return $this->getResponse()->setStatusCode('404');
			$errMessage = 'Error: ' . $_POST['Staff_Name'] . ' is already training in course named: ' . $_POST['CourseName'];
			show_error($errMessage, $status_code= 500);
			//show_error('Error: This Staff',$status_code= 500);
		}
		else
		{
			$this->muser->addData('StaffTraining',$dt);
			//return TRUE;
		}
		//redirect('training/assign');
	 }

	function getstaffassigntable(){
		$this->my_datatables
 		->select('StaffTraining.Training_ID, CourseName, StaffTraining.Staff_ID, Fullname, TRN_Achivement, Actual')
		->from('StaffTraining')
		->join('Training','Training.Training_ID=StaffTraining.Training_ID')
		->join('Staff','Staff.Staff_ID=StaffTraining.Staff_ID');
		echo $this->my_datatables->generate();
	}
	
	function getstaffassignbyid($courseId){
		$this->my_datatables
 		->select('StaffTraining.Training_ID, CourseName, StaffTraining.Staff_ID, Fullname, TRN_Achivement, Actual')
		->from('StaffTraining')
		->join('Training','Training.Training_ID=StaffTraining.Training_ID')
		->join('Staff','Staff.Staff_ID=StaffTraining.Staff_ID');
		echo $this->my_datatables->generate();
	}
	
	function unassign()
	{
		$trainingid = $_POST['id'];
		$staffid = $this->session->userdata('staff_id');
		$this->muser->delStaffTraining($trainingid, $staffid);
		$this->session->unset_userdata('staff_id');
	}

	function storestaffid(){
		$this->session->set_userdata('staff_id', $_POST['Staff_ID']);
	}
	
	function updatestafftraining(){
		$trainingid = $_POST['id'];
		$columnName = $_POST['columnName'];
		$data[$columnName] = $_POST['value'];
		$staffid = $this->session->userdata('staff_id');
		
		$this->muser->updateStaffTraining($trainingid, $staffid, $data);
		$this->session->unset_userdata('staff_id');
		echo $data[$columnName];
	}
	
	function individual(){
	 	$this->load->view('_header');
	    $this->load->view('training/individualtraining_view'); 
	    $this->load->view('_footer');
	 }
	
	function viewindividual(){
		// Select all records
		$this->my_datatables
	 		->select('StaffTraining.Staff_ID, Department.Department, Session.Session_ID, Staff.LastName, Staff.FirstName, Contract.Position, Training.TRN_Mode, CourseName, TRN_Provider, ActFromDate, ActToDate AS Duration, TRN_Achivement')
			->from('StaffTraining')
			->join('Training','Training.Training_ID=StaffTraining.Training_ID')
			->join('Staff','Staff.Staff_ID=StaffTraining.Staff_ID')
			->join('Contract','Contract.Staff_ID=Staff.Staff_ID')
			->join('Department','Department.Department_ID=Contract.Department_ID')
			->join('Session','Session.Session_ID=Contract.Session_ID');
		
		/*
		if (isset($_POST['Staff_ID'])){
					$staffid = $_POST['Staff_ID'];
					$datatable->where('StaffTraining.Staff_ID',$staffid);
					echo $this->my_datatables->generate();
					//return TRUE;
				}*/
						
		echo $this->my_datatables->generate();
	}
}